package com.samp.solr.file2solr2;


import com.samp.solr.file2solr.ErrorIntentionExtract;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.Map.Entry;

public class LastSoltAnalyze {

    private static SortedMap<String,SortedMap<String,Set<String>>> recordMap = new TreeMap();

    private static List<String> allIntentionList = new ArrayList<>();

    private static Set<String> stopWords = new HashSet<>();

    static {
        System.out.println("设置停用词");
        stopWords.add("");
    }

    public static void staticLastSoltDistrib(String sourceDir, String outFileName) throws Exception{
        Path dir = Paths.get(sourceDir);
        //通过Files.newDirectoryStream(dir)来遍历文件夹
        Integer index = 0;
        try( DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ){
            for(Path path : stream){
//                if( index >= 1){
//                    break;
//                }
                if( !Files.isDirectory(path) ){
                    System.out.println("process........"+path.getFileName());
                    String rawPath = sourceDir+path.getFileName();
                    staticLastSoltInFile(rawPath);
                    index++;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        //将文件保存起来。
        BufferedWriter writer = null;
        try {
            writer = Files.newBufferedWriter(Paths.get(outFileName), StandardCharsets.UTF_8);
            Set<Entry<String, SortedMap<String,Set<String>>>> entrys = recordMap.entrySet();
            for( Entry<String, SortedMap<String,Set<String>>> entry: entrys ){
                writer.write(entry.getKey());
                writer.newLine();
                SortedMap<String,Set<String>> lastSoltMap = entry.getValue();
                Set<Entry<String,Set<String>>> lastEntry = lastSoltMap.entrySet();
                for( Entry<String,Set<String>> lastSoltSet: lastEntry ){
                    writer.write("\t"+lastSoltSet.getKey());
                    writer.newLine();
                    writer.write("\t\t"+getSetString(lastSoltSet.getValue()));
                    writer.newLine();
                }

            }
            System.out.println("save done!");
            writer.flush();
            Collections.sort(allIntentionList);
            ErrorIntentionExtract.saveResultToFile(allIntentionList,"D:\\gcc-omcp\\all_intention_from_data.txt");
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally{
            if( writer != null ){
                writer.close();
            }
        }

    }

    private static String getSetString(Set<String> set){
        StringBuilder sb = new StringBuilder();
        for(String s:set){
            sb.append(s).append(";");
        }
        return sb.toString();
    }

    private static void staticLastSoltInFile(String rawPath)throws Exception{
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            while( (line = bufferReader.readLine()) != null ){
                String[] words = line.split("\t");
                if( !allIntentionList.contains(words[1]) ){
                    //找出有第5个槽位的记录
                    if( words[1].split("#").length > 4 ){
                        System.out.println(rawPath);
                        System.out.println("error 4:"+line);
                    }
                    allIntentionList.add(words[1]);
                }
                //意图如果有四个意图，则统计
                String[] splits = words[1].split("#");
                if( splits.length != 4 ){
//                    System.out.println("error data:"+line);
                    continue;
                }
//                String key = getSoltName(splits[0])+":"+getSoltName(splits[1])+":"+getSoltName(splits[2]);
                String key = splits[0]+"#"+splits[1]+"#"+splits[2];
                String lastSoltName = getSoltName(splits[3]);
                if( "default".equals(lastSoltName) ){
                    continue;
                }
                if( recordMap.get(key) == null ){
                    recordMap.put(key,new TreeMap<>());
                }
                if( recordMap.get(key).get(lastSoltName) == null ){
                    recordMap.get(key).put(lastSoltName, new HashSet<>());
                }
                String[] wordsSplits = words[0].split(" ");
                for(String word : wordsSplits ){
                    if( stopWords.contains(word) ){
                        continue;
                    }
                    recordMap.get(key).get(lastSoltName).add(word);
                }
                //如果没有意图，直接剔除掉。

            }
            System.out.println("current size = "+recordMap.size());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
    }

    private static  String getSoltName(String solt){
        return solt.split("=")[1];
    }

    public static void main(String[] args) {

        try{
            String sourceDir = "D:\\gcc-omcp\\new\\trans_1_15\\";
            String outDir = "D:\\gcc-omcp\\new\\data\\";
            String totalFile = "last_solt_anlayze.txt";
            staticLastSoltDistrib(sourceDir, outDir+totalFile);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
